package nju;

import java.text.NumberFormat;
import java.util.Locale;

public class JspWidget{
	
	private JspWidget() {}
	public static JspWidget $ = new JspWidget(); 
	
	public String actionLink(String label, String url, Object[] pars) {
		return actionLinks(label, url, pars, null);
	}
	
	public String actionLink(String label, String url, Object[] pars, String destDiv) {
		return actionLinks(label, url, pars, destDiv);
	}
	
	private String actionLinks(String label, String url, Object[] pars, String destDiv) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<a href=\"javascript:;\" onclick=\"var myAjax = new Ajax.Updater('");
		if(null == destDiv)
			sb.append("right");
		else 
			sb.append(destDiv);
		sb.append("','");
		sb.append(url);
		sb.append("' ,{method:'get', parameters:" );
		if(pars != null && pars.length > 1) {
			for(int i = 0; i < pars.length; i++) {
				if(i == pars.length - 1)
					sb.append(pars[i]);
				else {
					sb.append(pars[i]);
					sb.append("&");
				}
			}
		} else if(pars != null && pars.length == 1){
			sb.append(pars[0]);
		} else {
			sb.append("''");
		}
		sb.append(",evalScripts: true});\">");
		sb.append(label);
		sb.append("</a>");

		return sb.toString();
	}
	
	public String actionButton(String label, String url, Object[] pars) {
		return actionButtons(label, url, pars, null);
	}
	
	public String actionButton(String label, String url, Object[] pars, String destDiv) {
		return actionButtons(label, url, pars, destDiv);
	}
	
	private String actionButtons(String label, String url, Object[] pars, String destDiv) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<input type=\"button\" onclick=\"var myAjax = new Ajax.Updater('");
		if(null == destDiv)
			sb.append("right");
		else 
			sb.append(destDiv);
		sb.append("','");
		sb.append(url);
		sb.append("' ,{method:'get', parameters:" );
		if(pars != null && pars.length > 1) {
			for(int i = 0; i < pars.length; i++) {
				if(i == pars.length - 1)
					sb.append(pars[i]);
				else {
					sb.append(pars[i]);
					sb.append("&");
				}
			}
		} else if(pars != null && pars.length == 1){
			sb.append(pars[0]);
		} else {
			sb.append("''");
		}
		sb.append(",evalScripts: true});\" value=\""+ label +"\"/>");

		return sb.toString();
	}
	
	private String enc(double d) {
		return NumberFormat.getInstance(Locale.CHINA).format(d);
	}
	
	private String enc(boolean b) {
		return b ? "Yes" : "No";
	}
	
	public String enc(Object obj) {
		if (obj instanceof Double)
			return enc(((Double) obj).doubleValue());

		if (obj instanceof Boolean)
			return enc(((Boolean) obj).booleanValue());

		return obj == null ? "" : encodeHtml(obj.toString());
	}
	
	/**
	 * Encodes a given string into html format, taking care of &lt;, &gt;, &, ", '
	 * chars. Null values are absorbed and replaced with empty string.
	 *
	 * @author Jetty Yang
	 * @param value
	 * @return
	 */
	private String encodeHtml(String value) {
		if (value == null) {
			return "";
		}

		if (value.indexOf('<') < 0 && value.indexOf('>') < 0 && value.indexOf('&') < 0
				&& value.indexOf('"') < 0 && value.indexOf('\'') < 0) {
			return value;
		}

		char content[] = new char[value.length()];
		value.getChars(0, value.length(), content, 0);
		StringBuffer result = new StringBuffer(content.length + 50);
		for (int i = 0; i < content.length; i++) {
			switch (content[i]) {
			case '<':
				result.append("&lt;");
				break;
			case '>':
				result.append("&gt;");
				break;
			case '&':
				result.append("&amp;");
				break;
			case '"':
				result.append("&quot;");
				break;
			default:
				result.append(content[i]);
			}
		}
		return result.toString();
	}
	
	public String actionButtonSubmit(String label, String url, Object[] pars, String destDiv) {
		StringBuffer sb = new StringBuffer("");
		sb.append("<input type=\"button\" onclick=\"if(Yform.validateAllOnSubmit(this)) {var myAjax = new Ajax.Updater('");
		if(null == destDiv)
			sb.append("right");
		else 
			sb.append(destDiv);
		sb.append("','");
		sb.append(url);
		sb.append("' ,{method:'get', parameters:" );
		if(pars != null && pars.length > 1) {
			for(int i = 0; i < pars.length; i++) {
				if(i == pars.length - 1)
					sb.append(pars[i]);
				else {
					sb.append(pars[i]);
					sb.append("&");
				}
			}
		} else if(pars != null && pars.length == 1){
			sb.append(pars[0]);
		} else {
			sb.append("''");
		}
		sb.append(",evalScripts: true});}\" value=\""+ label +"\"/>");

		return sb.toString();
	}
}
